package org.lyc.lanqiao.past;

/**
 * 2023 真题 与或异或
 *
 * @author Liu Yicong
 * @date 2024/3/27
 */
public class BAndOrXorGates {
	static int sum = 0;
	static int[][] arr = {{1, 0, 1, 0, 1}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0}, {0, 0, 0, 0, 0},
			{0, 0, 0, 0, 0}};

	public static void main(String[] args) {
		for (int i = 0; i < 3; i++) {
			dfs(1, 0, i);
		}
		System.out.println(sum);
	}

	/**
	 * @param i    层数
	 * @param j    列数
	 * @param gate 门
	 */
	static void dfs(int i, int j, int gate) {

		if (gate == 0) {
			arr[i][j] = arr[i - 1][j] & arr[i - 1][j + 1];
		} else if (gate == 1) {
			arr[i][j] = arr[i - 1][j] | arr[i - 1][j + 1];
		} else if (gate == 2) {
			arr[i][j] = arr[i - 1][j] ^ arr[i - 1][j + 1];
		}

		//若arr[4][0]为1则计数sum++
		if (i == 4 && j == 0) {
			if (arr[i][j] == 1) {
				sum++;
			}
			return;
		}

		//移动到下一个位置
		if (j == 4 - i) {
			i++;
			j = 0;
		} else {
			j++;
		}

		//切换三种运算状态
		for (int k = 0; k < 3; k++) {
			dfs(i, j, k);
		}
	}

}
